package com.web.boot.web.pan;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.boot.global.model.User;
import com.web.boot.global.util.HttpContext;
import com.web.cms.pan.facade.UserInfoFacade;
import com.web.cms.pan.message.OperatorLogRequest;
import com.web.cms.pan.message.OperatorLogResponse;
import com.web.cms.pan.message.UserInfoResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户网盘信息控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Controller("panUserInfoController")
@Http(url = "/pan")
public class UserInfoController
{
	@Autowired
	private UserInfoFacade userInfoFacade;
	@Autowired
	private UserFacade userFacade;
	@Resource
	private HttpContext httpContext;
	
	private static DictParser dictParser = DictParser.getInstance();
	
	public UserInfoController()
	{
	}

	@Http(url = "/user_info_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		UserRequest userRequest = request.getQueryObject(UserRequest.class);
		Page<UserInfoResponse> page = this.listPage(userRequest, Const.PAGE_START);
		return View.createBuilder().setView("/pan/user_info_list")
						.put("model", userRequest)
						.put("map", dictParser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/user_info_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<UserInfoResponse> page = this.listPage(userRequest, start);
		return View.createBuilder().setView("/pan/user_info_list_grid")
						.put("page", page)
						.build();
	}

	private Page<UserInfoResponse> listPage(UserRequest request, int start)
	{
		Page<UserResponse> userPage = userFacade.listPage(request, start, Const.PAGE_SIZE);
		List<Integer> idList = userPage.getObjectList().stream().map(UserResponse::getId).collect(Collectors.toList());
		Map<Integer, UserInfoResponse> userInfoMap = userInfoFacade.getList(idList);
		List<UserInfoResponse> userInfoList = new ArrayList<>();
		for (UserResponse user : userPage.getObjectList())
		{
			UserInfoResponse response = userInfoMap.get(user.getId());
			if (response == null)
				continue;

			response.setName(user.getName());
			response.setNickname(user.getNickname());
			response.setMobile(user.getMobile());
			userInfoList.add(response);
		}
		Page<UserInfoResponse> userInfoPage = new Page<>(Const.PAGE_SIZE);
		userInfoPage.setCurPage(start);
		userInfoPage.setRows(userPage.getRows());
		userInfoPage.setObjectList(userInfoList);
		return userInfoPage;
	}

	@Http(url = "/operator_log_list", method = HttpMethod.GET)
	public View listLogGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		OperatorLogRequest operatorLogRequest = request.getQueryObject(OperatorLogRequest.class);
		boolean global = (operatorLogRequest.getUserId() == null ? true : false);
		if (operatorLogRequest.getUserId() == null)
		{
			operatorLogRequest.setUserId(user.getId());
		}
		Page<OperatorLogResponse> page = userInfoFacade.listLogPage(operatorLogRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/pan/operator_log_list")
						.put("global", global)
						.put("map", dictParser.getStringMap())
						.put("model", operatorLogRequest)
						.put("page", page)
						.build();
	}

	@Http(url = "/operator_log_list", method = HttpMethod.POST)
	public View listLogPost(HttpRequest request)
	{
		OperatorLogRequest operatorLogRequest = request.getPostObject(OperatorLogRequest.class);
		Page<OperatorLogResponse> page = userInfoFacade.listLogPage(operatorLogRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/pan/operator_log_list_grid")
				.put("page", page)
				.build();
	}

}
